package com.xzz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xzz.domain.UserCode;
import com.xzz.mapper.UserCodeMapper;
import com.xzz.service.IUserCodeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xzz.util.StrUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzz
 * @since 2022-08-31
 */
@Service
public class UserCodeServiceImpl extends ServiceImpl<UserCodeMapper, UserCode> implements IUserCodeService {

    @Override
    public UserCode adduserCode(Long userId){
        UserCode userCode = new UserCode();
        String code = StrUtils.getRandomString(4);
        userCode.setCode(code);
        userCode.setUserId(userId);
        insert(userCode);
        return userCode;
    }

    @Override
    public UserCode findByUserId(Long userId){
        EntityWrapper<UserCode> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        UserCode userCode = selectOne(wrapper);
        return userCode;
    }

    @Override
    public UserCode findByCode(String code){
        EntityWrapper<UserCode> wrapper = new EntityWrapper<>();
        wrapper.eq("code", code);
        UserCode userCode = selectOne(wrapper);
        return userCode;
    }

}
